 /**
 * Description: 系统角色控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:43:58
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.activation.sys.controller;

 import com.github.pagehelper.PageInfo;
 import com.metro.core.annotation.BusinessDesc;
 import com.metro.core.page.Page;
 import com.metro.core.vo.JsonResult;
 import com.metro.core.vo.QueryParams;
 import com.metro.core.vo.ResultCode;
 import com.metro.core.vo.inf.IResult;
 import com.metro.activation.common.beans.constant.DicTypeEnum;
 import com.metro.activation.common.utils.DicUtil;
 import com.metro.activation.sys.beans.vo.SysPermission;
 import com.metro.activation.sys.beans.vo.SysRole;
 import com.metro.activation.sys.beans.vo.SysRoleMenu;
 import com.metro.activation.sys.service.inf.ISysPermissionService;
 import com.metro.activation.sys.service.inf.ISysRoleMenuService;
 import com.metro.activation.sys.service.inf.ISysRoleService;
 import com.metro.activation.web.controller.BaseController;
 import org.apache.commons.collections.map.HashedMap;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.view.RedirectView;

 import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 import java.util.Map;

 /**
 * 系统角色控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Controller
@RequestMapping("/sysrole")
public class SysRoleController extends BaseController {
     //菜单编码
     private static final String menuCode = "sysrole";
     //模块描述
     private static final String MODULE_DESC = "系统角色";
     @Autowired
     private ISysRoleService sysRoleService;
     @Autowired
     private ISysRoleMenuService sysRoleMenuService;
     @Autowired
     private ISysPermissionService sysPermissionService;
     @Override
     protected String getViewDir() {
         return "sys/";
     }

     /**
      * 判断当前用户是否有权限编辑对应的数据
      */
     @RequestMapping(value = "validateUserHasRole", method = { RequestMethod.GET})
     @ResponseBody
     public JsonResult validateUserHasRole(String roleIds) {
         JsonResult jr = new JsonResult();
         try {
             IResult res = sysRoleService.validateUserHasRole(roleIds);
             jr.setResult(res);
         } catch (Exception ex) {
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

     /**
      * 权限分配
      */
     @SuppressWarnings("all")
     @RequestMapping(value = "rolePermission/{roleId}", method = { RequestMethod.GET})
     public ModelAndView permission(@PathVariable Long roleId, HttpServletRequest request){
         ModelAndView mav = new ModelAndView();
         mav.addObject("roleId", roleId);
         mav.setViewName(getView("sysrole_permission"));
         mav.addObject("operateList", DicUtil.getLabelValueList(DicTypeEnum.OPERATE));
         return mav;
     }

     /**
      * 查询ztree列表
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询角色权限")
     @RequestMapping(value = "queryRoleMenuTree", method = { RequestMethod.GET })
     @ResponseBody
     public JsonResult queryForTree(Long roleId) {
         JsonResult jr = new JsonResult();
         SysRole entity = sysRoleService.getById(roleId);
         QueryParams<SysPermission> params = new QueryParams<>();
         SysPermission permission = new SysPermission();
         permission.setSubsysCode(entity.getSubsysCode());
         params.setOrderColumn("id");
         List<SysPermission> permissions = sysPermissionService.queryForList(params);

         SysRoleMenu roleMenu = new SysRoleMenu();
         roleMenu.setRoleId(roleId);
         List<SysRoleMenu> roleMenus = sysRoleMenuService.queryForList(roleMenu);
         Map<String, Object> map = new HashedMap();
         map.put("permissions", permissions);
         map.put("roleMenus", roleMenus);
         map.put("entity", entity);
         jr.setData(map);
         jr.setResult(ResultCode.SUCCESS);
         return jr;
     }


     /**
      * 修改系统角色与权限的对应关系
      */
     @RequestMapping(value = "saveRoleMenu", method = { RequestMethod.POST})
     @ResponseBody
     public JsonResult saveRoleMenu(SysRoleMenu entity) {
         JsonResult jr = new JsonResult();
         try {
             sysRoleMenuService.save(entity);
             log.debug("修改系统角色与权限的对应关系success【{}】", entity);
             jr.setResult(ResultCode.SUCCESS);
         } catch (Exception ex) {
             log.error("修改系统角色与权限的对应关系", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

    /**
     * 去查询系统角色
     * 
     * @param sysRole 系统角色页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统角色列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(SysRole sysRole) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.setViewName(getView("sysrole_list"));
            mav.addObject("searchEntity", sysRole);
        } catch (Exception ex) {
            log.error("查询系统角色出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询系统角色
     *
     * @param sysRole 系统角色页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统角色列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(SysRole sysRole, Page page, QueryParams<SysRole> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(sysRole);
            queryParams.setOrderColumn("subsys_code, sort_num");
            PageInfo<SysRole> pageInfo = sysRoleService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询系统角色出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增系统角色
     * 
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增系统角色")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增系统角色出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改系统角色所需参数
     * @author xk
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("action", action);
        mav.setViewName(getView("sysrole_form"));
        mav.addObject("subsysCodes", DicUtil.getLabelValueList(DicTypeEnum.SUB_SYSTEM));
    }
    
    /**
     * 新增系统角色
     * 
     * @param entity 系统角色页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增系统角色")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysRole(SysRole entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysRoleService.save(entity);
            jr.setResult(res);
            log.debug("新增系统角色成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增系统角色出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除系统角色
     * 
     * @param id 系统角色页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除系统角色")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除系统角色id={}",id);
            IResult codeMsg = sysRoleService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除系统角色出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改系统角色
     * 
     * @param id 系统角色页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改系统角色")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            SysRole entity = sysRoleService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改系统角色", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改系统角色
     * 
     * @param entity 系统角色页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改系统角色")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysRole(SysRole entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysRoleService.update(entity);
            jr.setResult(res);
            log.debug("修改系统角色成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改系统角色出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去查看系统角色
     * 
     * @param id 系统角色页面表单对象唯一标识
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看系统角色详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", sysRoleService.getById(id));
            mav.setViewName(getView("sysrole_info"));
        } catch (Exception ex) {
            log.error("查看系统角色出错", ex);
        }
        return mav;
        
    }
    
}
